const path = require('path');
const webpack = require('webpack');
var jsonPackage = require('./package.json');
var ExtractTextPlugin = require("extract-text-webpack-plugin");

var vender = [];

for (var pro in jsonPackage.dependencies || {}) {
    vender.push(pro);
}
if (process.env.NODE_ENV.trim() == 'development') {
    vender = vender.concat([
        'faker',
    ]);
}
module.exports = {
    entry: {
        vendor: vender
    },
    output: {
        path: path.join(__dirname, 'build'),
        filename: '[name].dll.js',
        /**
         * output.library
         * window.${output.library}に定義される
         * 今回の場合、`window.vendor_library`になる
         */
        library: '[name]_library'
    },
    plugins: [

        //js 的压缩
        new webpack.optimize.UglifyJsPlugin({
            test: /(\.jsx|\.js)$/,
            exclude: /node_modules/,
            compress: {
                warnings: false
            },
            sourceMap: false,
        }),
        new webpack.DllPlugin({
            /**
             * path
             * manifestファイルの出力先
             * [name]の部分はentryの名前に変換される
             */
            path: path.join(__dirname, 'build', '[name]-manifest.json'),
            /**
             * name
             * どの空間（global変数）にdll bundleがあるか
             * output.libraryに指定した値を使えばよい
             */
            name: '[name]_library'
        }),

    ],
    module: {
        loaders: [
            // {
            //     test: require.resolve('jquery'),
            //     loader: 'expose?$!expose?jQuery'
            // },
            {
                test: /\.css$/,
                // exclude: /node_modules/,
                loader: ExtractTextPlugin.extract('style-loader', 'css-loader?sourceMap')
            },
            {
                test: /\.(less)$/,
                loader: ExtractTextPlugin.extract("style-loader", "css-loader!less-loader?sourceMap")
            },
            {
                test: /\.(js|jsx)$/,
                loader: 'babel',
                exclude: /node_modules/,
                query: {
                    plugins: [
                        "transform-es3-property-literals",
                        "transform-es3-member-expression-literals",
                        "transform-proto-to-assign",
                        ["transform-es2015-classes", {
                            "loose": true
                        }]
                    ],
                    presets: ['stage-0', 'react', 'es2015']
                },
            },
            {
                test: /\.json$/,
                loader: "json-loader"
            },
            {
                test: /\.(jpe?g|png|gif|svg)$/,
                loader: 'file-loader?limit=1024&name=images/[name].[ext]'
            },
            {
                test: /\.(woff|woff2)/,
                loader: 'file-loader?limit=100000&name=fonts/[name].[ext]'
            },
            {
                test: /\.(eot|ttf)/,
                loader: 'file-loader?name=fonts/[name].[ext]'
            },
            {
                test: /\.(svg)/,
                loader: 'file-loader?name=fonts/[name].[ext]'
            },
        ]
    },
    resolveLoader: {
        root: path.join(__dirname, 'node_modules')
    },
    resolve: {
        root: [
            path.join(__dirname, 'node_modules'),
            path.join(__dirname, 'bower_components'),
        ],
        // extensions: ['.css', '.js'],
        //配置别名，在项目中可以缩减引用路径
        alias: {}
    },
};